{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tkinter\n",
    "from tkinter import messagebox\n",
    "import os\n",
    "import os.path\n",
    "path=os.getenv('temp')\n",
    "filename=os.path.join(path,'info.txt')\n",
    "#创建应用程序窗口\n",
    "root=tkinter.Tk()\n",
    "#定义窗口大小\n",
    "root['height']=140\n",
    "root['width']=200\n",
    "#在窗口上创建标签组件\n",
    "labelName=tkinter.Label(root,text='User Name:',justify=tkinter.RIGHT,anchor='e',width=80)\n",
    "#把组件放置到窗口上指定区域\n",
    "labelName.place(x=10,y=5,width=80,height=20)\n",
    "#创建字符串变量和文本框组件，同时设置关联的变量\n",
    "#可以通过关联变量来获取文本框的内容\n",
    "varName=tkinter.StringVar(root,value='')\n",
    "entryName=tkinter.Entry(root,width=80,textvariable=varName)\n",
    "entryName.place(x=100,y=5,width=80,height=20)\n",
    "labelPwd=tkinter.Label(root,text='User Password:',justify=tkinter.RIGHT,anchor='e',width=80)\n",
    "labelPwd.place(x=10,y=35,width=80,height=20)\n",
    "#创建密码文本框\n",
    "varpwd=tkinter.StringVar(root,value='')\n",
    "entryPwd=tkinter.Entry(root,show='*',width=80,textvariable=varpwd)\n",
    "entryPwd.place(x=100,y=35,width=80,height=20)\n",
    "#尝试自动填写用户名和密码\n",
    "try:\n",
    "    with open(filename) as fp:\n",
    "        n,p=fp.read().strip().split(',')\n",
    "        varName.set(n)\n",
    "        varpwd.set(p)\n",
    "except:\n",
    "    pass\n",
    "#记住我，复选框\n",
    "# 记住我，复选框\n",
    "remember = tkinter.IntVar(root, value=1)\n",
    "checkRemember = tkinter.Checkbutton(root, text='Remember me?', variable=remember, onvalue=1, offvalue=0)\n",
    "checkRemember.place(x=30, y=70, width=120, height=20)\n",
    "\n",
    "#登录按钮事件处理函数\n",
    "def login():\n",
    "    #获取用户名和密码\n",
    "    name=entryName.get()\n",
    "    pwd=entryPwd.get()\n",
    "    if name=='admin'and pwd=='123456':                                                                                           马东升\n",
    "        tkinter.messagebox.showinfo(title='恭喜',message='登录成功!')\n",
    "        if remember.get()==1:\n",
    "            with open(filename,'w') as fp:\n",
    "                fp.write('%s,%s'%(name,pwd))\n",
    "    else:\n",
    "        try:\n",
    "            os.remove(filename)\n",
    "        except:\n",
    "            pass\n",
    "        else:\n",
    "            tkinter.messagebox.showerror(title='警告',message='用户名或密码错误!')\n",
    "buttonOk=tkinter.Button(root,text='Login',command=login)\n",
    "buttonOk.place(x=30,y=100,width=50,height=20)\n",
    "def cancel():\n",
    "    varName.set('')\n",
    "    varpwd.set('')\n",
    "buttonCancel=tkinter.Button(root,text='Cancel',command=cancel)\n",
    "buttonCancel.place(x=90,y=100,width=50,height=20)\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
